package lumis.portal.file;

import lumis.portal.PortalException;
import lumis.portal.PortalStringResource;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.propertybag.PropertyBag;
import lumis.portal.serviceinstance.ServiceInstanceConfig;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.converter.ConvertUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Validates the size of a file.
 * 
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
class SizeFileValidator implements IFileValidator
{
	/**
	 * Name of the property bag on instance service channel.
	 * 
	 * @since 6.0.0
	 */
	static final String PROPERTY_FILE_VALIDATOR_CLASS_SIZE = "lumis.portal.file.validation.size";

	private static ILogger logger = LoggerFactory.getLogger(HeightFileValidator.class);

	/**
	 * Validate the file using property bags defined on instance service
	 * channel. If the property wasn't defined, the validation will be ignored.
	 * 
	 * @param file The file to be validated.
	 * @throws FileValidationException If the validation has failed.
	 * @throws PortalException If any error occurs validating the metadata.
	 * @since 6.0.0
	 */
	public void validate(FileConfig file) throws PortalException
	{
		String serviceInstanceId = file.getServiceInstanceId();
		ServiceInstanceConfig serviceInstanceConfig = ManagerFactory.getServiceInstanceManager().get(serviceInstanceId, PortalTransactionFactory.getCurrentTransaction());
		String channelId = serviceInstanceConfig.getChannelId();
		ChannelConfig channelConfig = ManagerFactory.getChannelManager().get(SessionConfig.getCurrentSessionConfig(), channelId, PortalTransactionFactory.getCurrentTransaction());
		String propertyBagId = channelConfig.getPropertyBagId();

		PropertyBag propertyBag = ManagerFactory.getPropertyBagManager().get(propertyBagId);
		if (propertyBag.getValue(PROPERTY_FILE_VALIDATOR_CLASS_SIZE, null) != null)
		{
			try
			{
				long size = Long.parseLong(propertyBag.getValue(PROPERTY_FILE_VALIDATOR_CLASS_SIZE, null));

				// Make the validation
				if (size < file.getFileSize())
				{
					throw new FileValidationException("STR_FILE_VALIDATION_EXCEEDED_LIMIT;" + ConvertUtil.convert(String.class, ((float)size/1024f),  SessionConfig.getCurrentSessionConfig().getLocale(), "#,##0.#"), new PortalStringResource());
				}
			}
			catch (NumberFormatException e)
			{
				String message = "Invalid value of property bag '"+PROPERTY_FILE_VALIDATOR_CLASS_SIZE+"' on channel '"+channelId+"'. Invalid value: "+propertyBag.getValue(PROPERTY_FILE_VALIDATOR_CLASS_SIZE, null)+" range of values allowed: 0 - 9223372036854775807.";
				logger.error(message, e);
				throw new UnexpectedException(e);
			}

		}

	}

}
